package sg.edu.nus.iss.team8s.vmcs.control;
/**
 * 
 * @author Nie Xiaoyi
 *
 */
import sg.edu.nus.iss.team8s.vmcs.beans.CashStoreItem;
import sg.edu.nus.iss.team8s.vmcs.beans.Coin;
import sg.edu.nus.iss.team8s.vmcs.beans.StoreObject;
import sg.edu.nus.iss.team8s.vmcs.cache.CashStore;

public class ChangeGiver {
	private TransactionController tranController;
	private int change = 0;
	
	public ChangeGiver(TransactionController tranController){
		this.tranController = tranController;
	}

	public void resetChange(){
		
	}
	
	public boolean giveChange(int changeRequired){
		boolean result = true;
		CashStore cashStore = CashStore.getInstance();
		for(int i = cashStore.getStoreSize() - 1;i>0;i--){
			CashStoreItem item = (CashStoreItem)cashStore.getItem(i);
			if(item.getQuantity() == 0)
				continue;
			Coin coin = (Coin)item.getContent();
			int num = changeRequired/coin.getValue();
			if(num>0){
				MachineryController machineController = MachineryController.getInstance();
				result &= machineController.giveChange(i,num);
				changeRequired -= num*coin.getValue();
				change = num*coin.getValue();
			}
		}
		return result;
	}
	
	public void displayChangeStatue(){
		
	}
	
	public int getChange(){
		return change;
	}
	
	
}
